(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('apf.popup.discount.extendedMatDate',extendedMatDateController);
	extendedMatDateController.$inject = [ '$scope', '$q', '$uibModalInstance', 'Constants', 'ajaxServiceFactory', 'dialogsModule', 'gridService', 'modalService', 'layoutConfig', 'validatorConfig', 'authService' ];
	function extendedMatDateController($scope, $q, $uibModalInstance, Constants, ajaxServiceFactory, dialogsModule, gridService, modalService, layoutConfig, validatorConfig, authService) {
		var ctrl = this;
		ctrl.dialogsModule = dialogsModule;
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.extendedMatDate = {};	
		ctrl.extendedMatDate.label ="label.approgram.";
		ctrl.formName = 'extendedMatDateForm';
		var hasError = "N";
		
		ctrl.ok = function() {
			angular.forEach(ctrl.extendedMatDate, function(value, key) {
				if (!value) {
					delete ctrl.extendedMatDate[key];
				}
			});
			hasError = "N";
			if($scope.extendedMatDateForm) {
				$scope.extendedMatDateForm.$setSubmitted();
				if(!$scope.extendedMatDateForm.$valid) {
					dialogsModule.showErrorMessages($scope.extendedMatDateForm);
					return;
				}
			}
			
			var invGraceDayType = ctrl.extendedMatDate.invGraceDayType;
			if(ctrl.extendedMatDate.invGraceParam1){
				var invGraceParam1 = ctrl.extendedMatDate.invGraceParam1;
				var invGraceParam2 = ctrl.extendedMatDate.invGraceParam2;
				if("A" == invGraceDayType) {
					var param = 1;
					checkDay(invGraceParam1, param);
				} else if("W" == invGraceDayType){
					checkWeek(invGraceParam1);
				} else if("M" == invGraceDayType){
					checkMonth(invGraceParam1, invGraceParam2);
				}
			}
			if("Y" == hasError){
				return;
			}
			if (!dialogsModule.btnCallback || !dialogsModule.btnCallback['ok'] || dialogsModule.btnCallback['ok']([{"extendedMatDate" : angular.toJson(ctrl.extendedMatDate)}]) !== false) {
				$uibModalInstance.close();
			}
		};
		
		ctrl.close = function() {
			if (!dialogsModule.btnCallback || !dialogsModule.btnCallback['close'] || dialogsModule.btnCallback['close']() !== false) {
				$uibModalInstance.dismiss();
			}
		};
		function isNumber(pObj) {
			var strRef = "1234567890";
			for (var i = 0; i < pObj.length; i++) {
				var tempChar = pObj.substring(i, i + 1);
				if (strRef.indexOf(tempChar, 0) == -1) {
					return false;
				}
			}
			return true;
		}
		function checkDay(value, param) {
			var val1 = value;
			if (val1 != "") {
				var temp = replaceAll(val1, "-", ",").split(",");
				var tempName = ",LASTBIZ,FIRSTBIZ,";
				for (var i = 0; i < temp.length; i++) {
					if (isNumber(temp[i])) {
						if (temp[i] < 1 || temp[i] > 31) {
							hasError = "Y";
							modalService.openErrorDialogs({
								'messages' :  [ 'js.apf.param1.day.format.error']
							});
							return;
						}
					} else if ("FIRSTBIZ-LASTBIZ" == val1){
						hasError = "Y";
						modalService.openErrorDialogs({
							'messages' :  [ 'js.apf.param1.day.format.error']
						});
						return;
					}else if(temp.indexOf("FIRSTBIZ") != -1 && temp.indexOf("FIRSTBIZ") != 0){
						hasError = "Y";
						modalService.openErrorDialogs({
							'messages' :  [ 'js.apf.param1.day.format.error']
						});
						return;
					}else if(temp.indexOf("LASTBIZ") != -1 && temp.indexOf("LASTBIZ") != (temp.length - 1)){
						hasError = "Y";
						modalService.openErrorDialogs({
							'messages' :  [ 'js.apf.param1.day.format.error']
						});
						return;
					}else if (tempName.indexOf("," + temp[i] + ",") == -1) {
						hasError = "Y";
						modalService.openErrorDialogs({
							'messages' :  [ 'js.apf.param1.day.format.error']
						});
						return;
					}
				}
			}
		}
		function checkWeek(value) {
			var val1 = value;
			if (val1 != "") {
				var temp = replaceAll(val1, "-", ",").split(",");
				var tempName = ",LASTBIZ,FIRSTBIZ,";
				for (var i = 0; i < temp.length; i++) {
					if (isNumber(temp[i])) {
						if (temp[i] < 1 || temp[i] > 7) {
							hasError = "Y";
							modalService.openErrorDialogs({
								'messages' :  [ 'js.apf.param1.week.format.error']
							});
							return;
						}
					}  else if ("FIRSTBIZ-LASTBIZ" == val1){
						hasError = "Y";
						modalService.openErrorDialogs({
							'messages' :  [ 'js.apf.param1.week.format.error']
						});
						return;
					}else if(temp.indexOf("FIRSTBIZ") != -1 && temp.indexOf("FIRSTBIZ") != 0){
						hasError = "Y";
						modalService.openErrorDialogs({
							'messages' :  [ 'js.apf.param1.week.format.error']
						});
						return;
					}else if(temp.indexOf("LASTBIZ") != -1 && temp.indexOf("LASTBIZ") != (temp.length - 1)){
						hasError = "Y";
						modalService.openErrorDialogs({
							'messages' :  [ 'js.apf.param1.week.format.error']
						});
						return;
					}else if (tempName.indexOf("," + temp[i] + ",") == -1) {
						hasError = "Y";
						modalService.openErrorDialogs({
							'messages' :  [ 'js.apf.param1.week.format.error']
						});
						return;
					}
				}
			}
		}
		function checkMonth(value, param2) {
			var val1 = value;
			var val2 = param2;
			var messages = [];
			if (val1 != "") {
				var temp = replaceAll(val1, "-", ",").split(",");
				var tempName = "";
				for (var i = 0; i < temp.length; i++) {
					if (isNumber(temp[i])) {
						if (temp[i] < 1 || temp[i] > 12) {
							hasError = "Y";
							messages.push('js.apf.param1.month.format.error');
						}
						
					} else if (tempName.indexOf(temp[i], 0) == -1) {
						hasError = "Y";
						messages.push('js.apf.param1.month.format.error');
					}
				}
			}
			
			//check param2
			if (val2 != "") {
				var error2 = "N";
				var temp = replaceAll(val2, "-", ",").split(",");
				var tempName = ",LASTBIZ,FIRSTBIZ,";
				for (var i = 0; i < temp.length; i++) {
					if (isNumber(temp[i])) {
						if (temp[i] < 1 || temp[i] > 31) {
							error2 = "Y";
						}
					} else if(val1 == 2 && val1 > 29){
						error2 = "Y";
					}else if ("FIRSTBIZ-LASTBIZ" == val2){
						error2 = "Y";
					}else if(temp.indexOf("FIRSTBIZ") != -1 && temp.indexOf("FIRSTBIZ") != 0){
						error2 = "Y";
					}else if(temp.indexOf("LASTBIZ") != -1 && temp.indexOf("LASTBIZ") != (temp.length - 1)){
						error2 = "Y";
					}else if (tempName.indexOf("," + temp[i] + ",") == -1) {
						error2 = "Y";
					}
				}
				if("Y" == error2){
					hasError = "Y";
					messages.push('js.apf.param2.day.format.error');
				}
			}
			if(messages.length > 0){
				hasError = "Y";
				modalService.openErrorDialogs({
					'messages' :  messages
				});
				return;
			}
		}
		function replaceAll(strOrg, strFind, strReplace){
			var index = 0;
			while(strOrg.indexOf(strFind) != -1){
				 strOrg = strOrg.replace(strFind, strReplace);
				 index = strOrg.indexOf(strFind, index);
			}
			return strOrg;
		}
		
		ctrl.graceDayTypeChanged = function (){
			var invGraceDayType = ctrl.extendedMatDate.invGraceDayType;
			if("A" == invGraceDayType) {
				layoutConfig.gridConfigs.invGraceParam1.tooltip = ["tooltip.approgram.invgraceparam1"];
			}else if("W" == invGraceDayType){
				layoutConfig.gridConfigs.invGraceParam1.tooltip = ["tooltip.approgram.invgraceparam12"];
				layoutConfig.gridConfigs.invGraceParam1.label = "label.approgram.invgraceparam12";
			} else if("M" == invGraceDayType){
				layoutConfig.gridConfigs.invGraceParam1.tooltip = ["tooltip.approgram.invgraceparam13"];
				layoutConfig.gridConfigs.invGraceParam1.label = "label.approgram.invgraceparam13";
			}
		}
		
		function getGracedaytypeOptions() {
			var options = {
				"data" : [ {
					"label" : "NONE",
					"value" : "N"
				}, {
					"label" : "CALENDAR DAYS",
					"value" : "C"
				}, {
					"label" : "BUSINESS DAYS",
					"value" : "B"
				}, {
					"label" : "FIXED DATE",
					"value" : "A"
				}, {
					"label" : "FIXED DAY",
					"value" : "W"
				}, {
					"label" : "MONTHLY",
					"value" : "M"
				} ]
			};
			return options;
		}
		ctrl.init = function() {
			ctrl.gracedaytypeOptions = getGracedaytypeOptions();
			ctrl.extendedMatDate.invGraceDayType = "N";
			if(dialogsModule.params.extendedMatDate){
				ctrl.extendedMatDate = angular.fromJson(dialogsModule.params.extendedMatDate);
			};
		};
	}
})(angular);
